﻿Imports System.Data.OleDb


Public Class postsAdd
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then


        Else
            Response.Redirect("posts.aspx?id=" & Request.QueryString("id"))
        End If



        Dim sql5 As String = "SELECT * FROM PostsRatingQuery WHERE DiscussionID=@f1"

        Dim conn5 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        conn5.Open()

        Dim cmd5 As OleDbCommand = New OleDbCommand(sql5, conn5)

        cmd5.Parameters.AddWithValue("@f1", Request.QueryString("id"))

        Dim dataReader = cmd5.ExecuteReader

        dataReader.Read()



        Try
            If Not IsDBNull(dataReader("AvgOfRating")) Then lblAverageRatings.Text = dataReader("AvgOfRating")
        Catch ex As Exception
            lblAverageRatings.Text = 0
        End Try


        If System.Web.HttpContext.Current.User.Identity.IsAuthenticated Then

        Else
            Response.Redirect("PostsAdd.aspx")
        End If


        Dim int As Single = CInt(lblAverageRatings.Text)


        If int >= 3 Then
            imgSmileyFace.Visible = True
            imgAngryFace.Visible = False

        Else
            imgAngryFace.Visible = True
            imgSmileyFace.Visible = False
        End If












    End Sub

    Protected Sub btnpost_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnpost.Click

        If txtpost.Text = "" Then
            MsgBox("You have not entered a post, please check and try again")
        Else
            Dim sql As String = "INSERT INTO Posts (PostBody,DatePosted,TimePosted,UserIDFK,DiscussionIDFK) VALUES (@f1,@f2,@f3,@f4,@f5)"
            Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

            cmd.Parameters.AddWithValue("@f1", txtpost.Text)
            cmd.Parameters.AddWithValue("@f2", Now.Date.ToString("dd/MM/yyyy"))
            cmd.Parameters.AddWithValue("@f3", DateTime.Now.ToString("t"))
            cmd.Parameters.AddWithValue("@f4", Membership.GetUser(User.Identity.Name).ProviderUserKey)
            cmd.Parameters.AddWithValue("@f5", Request.QueryString("id"))





            conn.Open()


            cmd.ExecuteNonQuery()

            conn.Close()

            cmd.Dispose()

            conn.Dispose()

            Response.Redirect("postsAdd.aspx?id=" & Request.QueryString("id"))
        End If


        

    End Sub

    Protected Sub txtpost_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtpost.TextChanged

    End Sub

    Protected Sub ListView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles ListView1.SelectedIndexChanged

    End Sub

    Protected Sub btnRate_Click(sender As Object, e As EventArgs) Handles btnRate.Click

        Dim sql2 As String = "DELETE FROM UserRatingPosts WHERE Username=@f1 AND DiscussionID=@f2"
        Dim conn2 As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)
        conn2.Open()

        Dim cmd2 As OleDbCommand = New OleDbCommand(sql2, conn2)

        cmd2.Parameters.AddWithValue("@f1", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd2.Parameters.AddWithValue("@f3", Request.QueryString("id"))

        cmd2.ExecuteNonQuery()
        conn2.Close()
        cmd2.Dispose()
        conn2.Dispose()









        Dim sql As String = "INSERT INTO UserRatingPosts (Rating,Username,DiscussionID) VALUES (@f1,@f2,@f3)"

        Dim conn As OleDbConnection = New OleDbConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

        conn.Open()

        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)

        cmd.Parameters.AddWithValue("@f1", Rating1.CurrentRating)
        cmd.Parameters.AddWithValue("@f2", Membership.GetUser(User.Identity.Name).ProviderUserKey)
        cmd.Parameters.AddWithValue("@f3", Request.QueryString("id"))

        cmd.ExecuteNonQuery()
        conn.Close()
        cmd.Dispose()
        conn.Dispose()


        Response.Redirect("PostsAdd.aspx?id=" & Request.QueryString("id"))









    End Sub
End Class